package com.longrise.android.byjk.plugins.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.main.MainActivity;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.Control;
import com.longrise.common.utils.ActivityUtil;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.android.byjk.utils.PhoneUtils;
import com.longrise.android.byjk.widget.view.InputBoxView;

/**
 * Created by callxkj on 2017/3/23.
 * 手机号登录 快捷登录
 */

public class LoginByPhnumActivity extends BaseActivity implements View.OnClickListener, TextWatcher {

    private Toolbar mToolbar;
    private TextView mTvTitle;
    private TextView mTvgetvf;
    private EditText mEtphone;
    private InputBoxView mIbvvfcode;
    private Button mBtlogin;
    private ImageView mForgetPwd;
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            mTvgetvf.setText("重新获取(" + millisUntilFinished / 1000 + "s)");

        }

        @Override
        public void onFinish() {
            getVftoNorm();
        }
    };

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_loginbyphnum;
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mTvTitle = (TextView) findViewById(R.id.bb_toolbar_middle_title);
        mTvTitle.setText(AppUtil.getString(R.string.login_byph_title));
        mTvTitle.setVisibility(View.VISIBLE);
        mForgetPwd = (ImageView) findViewById(R.id.forgetpasword_pic1);

        mTvgetvf = (TextView) findViewById(R.id.forgetpasword_pic2);
        mEtphone = (EditText) findViewById(R.id.forgetpasword_text);
        mEtphone.setHint(AppUtil.getString(R.string.phone_num_hint));
        mEtphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String phone = mEtphone.getText().toString().trim();
                if(!TextUtils.isEmpty(phone)&&!"".equals(phone)){
                    mForgetPwd.setSelected(true);
                }else{
                    mForgetPwd.setSelected(false);
                }

            }
        });

        mIbvvfcode = (InputBoxView) findViewById(R.id.loginbyph_vfcode);

        mBtlogin = (Button) findViewById(R.id.loginbyph_login);
        mBtlogin.setEnabled(false);

        initEvent();

    }

    private void initEvent() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });
        mBtlogin.setOnClickListener(this);
        mTvgetvf.setOnClickListener(this);
        mEtphone.addTextChangedListener(this);
        mIbvvfcode.getEditText().addTextChangedListener(this);
        mIbvvfcode.setRightClickMode(InputBoxView.USERIF);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 关闭当前页面
     */
    private void close() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginbyph_login: //登录
                toLogin();
                break;
            case R.id.forgetpasword_pic2: //获取验证码
                toGetVfCode();
                break;
        }
    }

    /**
     * 让获取验证码的按钮恢复正常状态
     */
    private void getVftoNorm() {
        mTvgetvf.setClickable(true);
        mTvgetvf.setText("获取验证码");
        mTvgetvf.setTextColor(Color.parseColor("#FF9900"));
    }

    /**
     * 获取短信验证码
     * checktype:验证类别("1":通过手机号注册;"2":通过手机号登录;"3":开通手机号快捷登录;)
     */
    private void toGetVfCode() {
        String mobile = mEtphone.getText().toString().trim(); //手机号码
        if (TextUtils.isEmpty(mobile)) {
            showToast(AppUtil.getString(R.string.phonenum_isnull));
            return;
        }
        if (!PhoneUtils.isPhoneNumber(mobile)) {
            showToast(AppUtil.getString(R.string.txsjh));
            return;
        }
        mTvgetvf.setClickable(false);
        mTvgetvf.setTextColor(Color.parseColor("#666666"));
        showLoadingDialog();
        GetMobileVfhelper.getInstance().getMobileVfCode(mContext, mobile, "2", new GetMobileVfhelper.MobileVfCodeCallback() {
            @Override
            public void onSuccess(String resultStatus) {
                if ("1".equals(resultStatus)) {
                    timer.start();
                } else {
                    getVftoNorm();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                dismissLoadingDialog();
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                getVftoNorm();
            }
        });
    }

    /**
     * 登录
     */
    private void toLogin() {
        String mobile = mEtphone.getText().toString().trim(); //手机号码
        String mobilecode = mIbvvfcode.getText(); //短信验证码
        showLoadingDialog();
        UserController.getInstance().mobileLogin(mContext, mobile, mobilecode, new UserCallback() {
            @Override
            public void onSuccess(int status, String des) {
                //用手机号登陆后 将登陆页的用户名置空.
                CacheUtils.setString(Control.USERNAME, "");
                showToast(des);
                if (status == UserCallback.HOME_PAGE) {
                    ActivityUtil.removeActivity(LoginByPhnumActivity.this);
                    ActivityUtil.finishAllAcivity();
                    startActivity(new Intent(mContext, MainActivity.class));
                    finish();
                }
            }

            @Override
            public void onFail(String des) {
                showToast(des);
            }

            @Override
            public void onFinish() {
                dismissLoadingDialog();
            }
        });
    }

    //-------------输入框字符变化----------------
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        boolean b = checkText();
        if (b) {
            mBtlogin.setEnabled(true);
        } else {
            mBtlogin.setEnabled(false);
        }
    }
    //------------------------------------------

    /**
     * 检查是否全部填写
     *
     * @return 全部有填写 返回true
     */
    private boolean checkText() {
        //手机号
        String phoneNum = mEtphone.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNum)) {
            return false;
        }
        //短信验证码
        String mIbvVfcodeText = mIbvvfcode.getText();
        if (TextUtils.isEmpty(mIbvVfcodeText)) {
            return false;
        }
        return true;
    }

}
